package com.jse;

import javax.sql.DataSource;

import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.lang.Lang;
import org.nutz.plugins.cache.dao.CachedNutDaoExecutor;
import org.nutz.plugins.cache.dao.api.DaoCacheProvider;
import org.nutz.plugins.cache.dao.impl.provider.EhcacheDaoCacheProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.druid.pool.DruidDataSource;
import com.jse.dao.Dao;

import jdk.nashorn.api.scripting.NashornScriptEngine;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import net.sf.ehcache.CacheManager;

@Configuration
@ComponentScan(basePackages = "com.jse")
@EnableAspectJAutoProxy
//@EnableCaching<!-- 启用缓存注解 -->
public class AppConfig {
	
    @Bean(initMethod="init") public BeetlGroupUtilConfiguration beetlConfig() {return new BeetlGroupUtilConfiguration();}
    
    @Bean public PropertiesProxy conf() {return new PropertiesProxy("jse.properties");}
    
    @Bean(initMethod="init",destroyMethod="close") public DataSource dataSource() {return conf().make(DruidDataSource.class, "db.");}
    
    @Bean public CacheManager cacheManager() {return CacheManager.create();}
    
    @Bean public JdbcTemplate jdbcTemplate() {return new JdbcTemplate(dataSource());}
    
    @Bean(initMethod="init") public DaoCacheProvider cacheProvider(){
    	EhcacheDaoCacheProvider cacheProvider=new EhcacheDaoCacheProvider();
    	cacheProvider.setCacheManager(cacheManager());
    	return cacheProvider;
    }
    @Bean public Dao dao() {
    	Dao dao=new Dao();
    	dao.setDataSource(dataSource());
    	dao.setJdbcTemplate(jdbcTemplate());
    	CachedNutDaoExecutor cacheExecutor=new CachedNutDaoExecutor();
    	cacheExecutor.setCacheProvider(cacheProvider());
		cacheExecutor.setCachedTableNames(Lang.set(conf().get("cache.table").split(",")));
		dao.setExecutor(cacheExecutor);
		return dao;
    }
    
    @Bean public NashornScriptEngineFactory nashornFactory() {return new NashornScriptEngineFactory();}
    @Bean public NashornScriptEngine scriptEngine() {
    	return (NashornScriptEngine) nashornFactory().getScriptEngine("--language=es6","--optimistic-types=[true]");
    }
    

}